// pages/blindBasicInfo/blindBasicInfo.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    formlist: [
      { isrequire: false, label: '昵称', key: 'nickName', valueType: 'text', tips: '请输入', type: 'input' },
      { isrequire: false, label: '性别', key: 'sex', valueType: 'number', index: 0, tips: '请选择', type: 'select', data: ['男','女'] },
      {
        isrequire: false, label: '身高', key: 'height', index: [0, 0], valueType: 'digit', tips: '请输入', type: 'input'},
      // { isrequire: false, label: '出生年月', key: 'birthday', valueType: 'text', tips: '请选择', type: 'select', mode: 'date'},
      // { isrequire: false, label: '居住地', key: 'address', valueType: 'text', tips: '请选择', type: 'select', mode: 'region' }, 
      { isrequire: false, label: '情感状况', key: 'emotionStatus', valueType: 'text', tips: '请输入', type: 'input'},
      { isrequire: false, label: '学历', key: 'degree', valueType: 'text', tips: '请选择', type: 'select', data: ['博士后', '博士 ', '硕士', '本科', '专科', '高中', '高中以下']},
      { isrequire: false, label: '工作单位', key: 'workUnit', valueType: 'text', tips: '请输入', type: 'input'},
      { isrequire: false, label: '单位类别', rangeKey: 'name', key: 'unitType', valueType: 'text', tips: '请选择', type: 'select', data: [] },
      { isrequire: false, label: '月收入', key: 'monthIncome', index: [0, 0], valueType: 'text', tips: '请选择', mode: 'multiSelector',type: 'select', data: [] },
      { isrequire: false, label: '婚姻状况', key: 'marriageStatus', valueType: 'text', tips: '请选择', type: 'select', data: ['未婚', '离异'] },
      { isrequire: false, label: '购房情况', key: 'purchaseStatus', valueType: 'text', tips: '请选择', type: 'select', data: ['无', '有']},
      { isrequire: false, label: '购车情况', key: 'carBuyingStatus', valueType: 'text', tips: '请选择', type: 'select', data: ['无', '有'] }
    ],
    formData: {
      nickName:'',
      height: "",
      sex:'',
      degree: '',
      unitType: '',
      monthIncome: '',
      marriageStatus: '',
      purchaseStatus: '',
      carBuyingStatus:'',
      // address:'',
      birthday:'',
      emotionStatus:'',
      workUnit:''
    },
    showData:{
      degree:'',
      age:'',
      unitType: '',
      monthIncome: '',
      marriageStatus: '',
      purchaseStatus: '',
      carBuyingStatus: '',
      // address:''
    },
    active: '',
    canSubmit: false,
  },
  bindPickerChange: function (e) {
    var item = e.currentTarget.dataset.item;
    if (item.data) {
      if (item.key=='degree'){
        this.data.formData[item.key] = parseInt(e.detail.value)+1;
        this.data.showData[item.key] = item.data[e.detail.value]
      }else{
        if (item.data[e.detail.value].id) {
          this.data.formData[item.key] = item.data[e.detail.value].id;
          this.data.showData[item.key] = item.data[e.detail.value][item.rangeKey]
        }else{
          this.data.formData[item.key] = e.detail.value;
          this.data.showData[item.key] = item.data[e.detail.value]
        }
      }
    } else {
      this.data.formData[item.key] = e.detail.value
    }
    this.setData({
      ['formData.' + item.key]: this.data.formData[item.key],
      ['showData.' + item.key]: this.data.showData[item.key],
    })
  },
  bindMultiPickerChange: function (e) {
    var item = e.currentTarget.dataset.item;
    var index = e.currentTarget.dataset.index;
    this.data.formlist[index].index = e.detail.value;
    // this.data.showData[item.key] = item.data[0][e.detail.value[0]].value + '-' + item.data[1][e.detail.value[1]].value;
    // this.data.formData[item.key] = item.data[0][e.detail.value[0]].id + '-' + item.data[1][e.detail.value[1]].id;
    this.data.formData[item.key] = item.data[0][e.detail.value[0]] + '-' + item.data[1][e.detail.value[1]];
    this.data.showData[item.key] = item.data[0][e.detail.value[0]] + '-' + item.data[1][e.detail.value[1]];
    this.setData({
      ['formData.'+item.key]: this.data.formData[item.key],
      ['showData.' + item.key]: this.data.showData[item.key],
    })
  },
  bindColumnchange:function(e){
    var item = e.currentTarget.dataset.item;
    var index = e.currentTarget.dataset.index;
    if (e.detail.column==0){
        var arr = [];
        arr =arr.concat(item.data[0].slice(e.detail.value, item.data[0].length)); 
        this.setData({
          ['formlist[' + index + '].data[1]']: arr
        })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    var that = this;
    wx.getStorage({
      key: 'basicInfo',
      success: function (res) {
        if (res.data) {
          for (var j in that.data.formData) {
            if (res.data[j] !== '' && res.data[j] != undefined && res.data[j] != null) {
              if (j == 'address') {
                that.data.formData[j] = res.data[j].split(',');
              } else {
                if (res.data[j] == -1) {
                  that.data.formData[j] = '';
                } else {
                  that.data.formData[j] = res.data[j];
                }
              }
            }
          }
        }
        that.setData({
          formData: that.data.formData
        })
        that.getUnitType();
        that.setSelectValue('sex', 1);
        that.setSelect('degree', 4, true);
        that.setSelect('marriageStatus', 8);
        that.setSelect('purchaseStatus', 9);
        that.setSelect('carBuyingStatus', 10);
        that.getMonthIncome();
        wx.removeStorage({ key: 'basicInfo' });
      }
    })
  },
  setSelect: function (key, index,flag) {
    if (this.data.formData[key] !== '') {
      var ind= flag ? this.data.formData[key] - 1 : this.data.formData[key];
      this.setData({
        ['formlist[' + index + '].index']: ind,
        ['showData.' + key]: this.data.formlist[index].data[ind]
      })
    }
  },
  setSelectValue: function (key, index) {
    if (this.data.formData[key] !== '') {
      var that=this;
      this.data.formlist[index].data.forEach(function(ele,j){
        if (ele == that.data.formData[key]){
          that.setData({
            ['formlist[' + index + '].index']: j,
            ['showData.' + key]: ele
          })
        }
      })
    }
  },
  getMonthIncome: function () {
    var monthIncomeArr = [];
    for (var i = 1; i <= 20; i ++) {
      monthIncomeArr.push(i * 1000);
    }
    monthIncomeArr.push('20000以上')
    if (this.data.formData.monthIncome != '') {
      if (this.data.formData.monthIncome.indexOf('-') >= 0) {
        var monthIncome = this.data.formData.monthIncome.split('-');
        var heightiIndex = [0, 0];
        monthIncomeArr.forEach(function (ele, index) {
          if (ele == monthIncome[0]) {
            heightiIndex[0] = index;
          }
          if (ele == monthIncome[1]) {
            heightiIndex[1] = index;
          }
        })
        this.setData({
          ['formlist[7].index']: heightiIndex,
          'showData.monthIncome': this.data.formData.monthIncome
        })
      }
    }
    this.setData({
      ['formlist[7].data']: [monthIncomeArr, monthIncomeArr]
    })
  },
  getUnitType: function () {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyIndustryResource/all', //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {},
      success: function (res) {
        if (res.data.success) {
          res.data.data.forEach(function (ele, index) {
            if (that.data.formData.unitType != '') {
              if (that.data.formData.unitType == ele.id) {
                that.setData({
                  ['formlist[6].index']: index,
                  'showData.unitType': ele.name
                })
              }
            }
          })
          that.setData({
            ['formlist[6].data']: res.data.data
          })
        }
      }
    })
  },
  changeTextarea: function (e) {//输入框值改变事件
    var key = e.target.dataset.value;
    // if (key =='height'){
    //   this.data.formData[key] = parseInt(e.detail.value).toFixed(2);
    //   this.setData({
    //     formData: this.data.formData
    //   })
    // }
    // else{
    //   this.data.formData[key] = e.detail.value;
    // }
    
  },
  saveBasicInfo: function (e) {
    var form=e.detail.value;
    var param = {};
    var flag=true;
    for (var j in this.data.formData) {
      if (this.data.formData[j] === '') {
        flag = false;
      }
      if (form[j]!=undefined){
        param[j] = form[j];
        this.data.formData[j] = form[j];
      }else{
        param[j] = this.data.formData[j];
      }
    }
    // if (param.address != '') {
    //   param.address = param.address.join(',');
    // }
    param.userId = this.data.userInfo.id;
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'xqUserBasicInfo/update', //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: param,
      success: function (res) {
        if (res.data.success) {
          wx.navigateBack({
            delta:1
          })
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  saveCanel:function(e){
    wx.navigateBack({
      delta: 1
    })
  },
  changeValue() {
    var flag = true;
    var item = this.data.formData;
    if ((item.brandCar == '') || (item.plateNumber == '') || (item.carColor == '') || (item.seat == '')) {
      flag = false;
    }
    this.setData({
      canSubmit: flag
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})